package com.hh85.movetab;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;


import android.app.Notification;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hh85.slidetab.IClickMenu;
import com.hh85.slidetab.TabView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabView tabView;
    private ViewPager  viewPager;
    private List<Fragment> viewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabView = (TabView)findViewById(R.id.id_tab);
        ArrayList<String> data = new ArrayList<>();
        data.add("最新");
        data.add("热门");
        data.add("推荐");
        viewList = new ArrayList<>();

        for (int i=0;i<3;i++){
            HomeFragment homeFragment = new HomeFragment();
         Bundle bundle = new Bundle();
           bundle.putString("name","page"+i);
            homeFragment.setArguments(bundle);
            viewList.add(homeFragment);
        }
        tabView.setTabMenuText(data);
        tabView.setOnClickMenuListener(new IClickMenu() {
            @Override
            public void clickMenu(int pos) {
                Log.i("TAG",pos+"");
                viewPager.setCurrentItem(pos);
            }
        });

        viewPager = (ViewPager)findViewById(R.id.id_viewpager);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return viewList.get(position);
            }


            @Override
            public int getCount() {
                return viewList.size();
            }
        });

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                tabView.scroll(position,positionOffset);
            }

            @Override
            public void onPageSelected(int position) {

                tabView.setPos(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
